package org.example.managere.service.feibiao.impl;

import lombok.RequiredArgsConstructor;
import org.example.managere.dto.feibiao.AmKnowledgeEntityDto;
import org.example.managere.entity.feibiao.AmKnowledgeEntity;
import org.example.managere.repository.feibiao.AmKnowledgeEntityRepository;
import org.example.managere.service.feibiao.AmKnowledgeEntityService;
import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 知识图谱实体服务实现类 - 电机向量知识库
 */
@Service
@RequiredArgsConstructor
public class AmKnowledgeEntityServiceImpl implements AmKnowledgeEntityService {
    
    private final AmKnowledgeEntityRepository repository;
    private final ModelMapper modelMapper;
    
    /**
     * 将实体转换为DTO
     */
    private AmKnowledgeEntityDto convertToDto(AmKnowledgeEntity entity) {
        return modelMapper.map(entity, AmKnowledgeEntityDto.class);
    }
    
    /**
     * 将DTO转换为实体
     */
    private AmKnowledgeEntity convertToEntity(AmKnowledgeEntityDto dto) {
        return modelMapper.map(dto, AmKnowledgeEntity.class);
    }
    
    @Override
    @Transactional
    public AmKnowledgeEntityDto save(AmKnowledgeEntityDto dto) {
        AmKnowledgeEntity entity = convertToEntity(dto);
        // 入库时间在entity的@PrePersist方法中自动设置
        AmKnowledgeEntity savedEntity = repository.save(entity);
        return convertToDto(savedEntity);
    }
    
    @Override
    @Transactional(readOnly = true)
    public Optional<AmKnowledgeEntityDto> findById(String entityId) {
        return repository.findById(entityId)
                .map(this::convertToDto);
    }
    
    @Override
    @Transactional(readOnly = true)
    public Optional<AmKnowledgeEntityDto> findByEntityName(String entityName) {
        return Optional.ofNullable(repository.findByEntityName(entityName))
                .map(this::convertToDto);
    }
    
    @Override
    @Transactional
    public AmKnowledgeEntityDto update(AmKnowledgeEntityDto dto) {
        // 确保记录存在
        Optional<AmKnowledgeEntity> existingEntityOpt = repository.findById(dto.getEntityId());
        
        if (existingEntityOpt.isPresent()) {
            // 实体存在，保留原有的录入时间
            AmKnowledgeEntity existingEntity = existingEntityOpt.get();
            
            // 创建并配置更新后的实体，确保保留原始录入时间
            AmKnowledgeEntity updatedEntity = convertToEntity(dto);
            updatedEntity.setEntryTime(existingEntity.getEntryTime());
            
            // 保存更新后的实体
            AmKnowledgeEntity savedEntity = repository.save(updatedEntity);
            return convertToDto(savedEntity);
        } else {
            // 实体不存在，创建新的
            return save(dto);
        }
    }
    
    @Override
    @Transactional
    public void deleteById(String entityId) {
        repository.deleteById(entityId);
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<AmKnowledgeEntityDto> findAll() {
        return repository.findAll().stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }
    
    @Override
    @Transactional(readOnly = true)
    public Page<AmKnowledgeEntityDto> findAll(Pageable pageable) {
        return repository.findAll(pageable)
                .map(this::convertToDto);
    }
    
    @Override
    @Transactional(readOnly = true)
    public Page<AmKnowledgeEntityDto> searchByKeyword(String keyword, Pageable pageable) {
        return repository.searchByKeyword(keyword, pageable)
                .map(this::convertToDto);
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<AmKnowledgeEntityDto> findByEntityNameContaining(String entityName) {
        return repository.findByEntityNameContaining(entityName).stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<AmKnowledgeEntityDto> findByEntityType(String entityType) {
        return repository.findByEntityType(entityType).stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<AmKnowledgeEntityDto> findByEntityTypeContaining(String entityType) {
        return repository.findByEntityTypeContaining(entityType).stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<AmKnowledgeEntityDto> findByEntryTimeBetween(Date startDate, Date endDate) {
        return repository.findByEntryTimeBetween(startDate, endDate).stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }
    
    @Override
    @Transactional(readOnly = true)
    public long countByEntityType(String entityType) {
        return repository.countByEntityType(entityType);
    }
} 